package com.yonyou.cyxdms.sysAuth.service.service.itemPush.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.service.http.HttpRequestService;
import com.yonyou.cyx.function.bean.dto.http.RestServiceRequestDto;
import com.yonyou.cyx.function.bean.dto.http.RestServiceResponseDto;
import com.yonyou.cyxdms.sysAuth.service.entity.cms.GetUserInfoByjwtVO;
import com.yonyou.cyxdms.sysAuth.service.entity.cms.YunyangVO;
import com.yonyou.cyxdms.sysAuth.service.entity.itemPush.ItemPushPO;
import com.yonyou.cyxdms.sysAuth.service.mapper.UserMapper;
import com.yonyou.cyxdms.sysAuth.service.mapper.itemPush.ItemPushMapper;
import com.yonyou.cyxdms.sysAuth.service.service.UserService;
import com.yonyou.cyxdms.sysAuth.service.service.itemPush.ItemPushService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonyou.cyxdms.sysAuth.service.dto.itemPush.ItemPushDTO;
import com.yonyou.cyx.function.exception.DALException;
import com.yonyou.cyx.function.utils.common.StringUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhangtan
 * @since 2020-11-19
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ItemPushServiceImpl extends ServiceImpl<ItemPushMapper, ItemPushPO> implements ItemPushService {
    //日志对象
    private static final Logger logger = LoggerFactory.getLogger(ItemPushServiceImpl.class);
    @Autowired
    ItemPushMapper itemPushMapper;

    @Autowired
    UserService userService;

    @Autowired
    UserMapper userMapper;

    @Autowired
    HttpRequestService httpService;

    /**
     * 分页查询对应数据
     *
     * @param page           分页对象
     * @param queryCondition 查询条件
     * @return com.baomidou.mybatisplus.core.metadata.IPage<java.util.Map<java.lang.String,java.lang.Object>>
     * @author zhangtan
     * @since 2018/7/22 0022
     */
    @Override
    @SuppressWarnings("unchecked")
    @Transactional(readOnly = true)
    public IPage<Map<String, Object>> selectPageBysql(Page page, Map<String, Object> queryCondition) {
        // 获取总条数
        page.setRecords(itemPushMapper.selectPageBySql(page, queryCondition));
        return page;
    }

    /**
     * 根据查询条件返回结果集
     *
     * @param queryCondition 查询条件
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @author zhangtan
     * @since 2018/7/22 0022
     */
    @Override
    @Transactional(readOnly = true)
    public List<Map<String, Object>> selectListBySql(Map<String, Object> queryCondition) {
        return itemPushMapper.selectListBySql(queryCondition);
    }

    /**
     * 根据查询条件返回结果集
     *
     * @param id 主键ID
     * @return java.util.List<java.util.Map < java.lang.String , java.lang.Object>>
     * @author zhangtan
     * @since 2018/7/22 0022
     */
    @Override
    @Transactional(readOnly = true)
    public Map<String, Object> getById(Long id) {
        ItemPushPO itempushpo = itemPushMapper.selectById(id);
        if (itempushpo != null) {
            return itempushpo.toMaps();
        } else {
            throw new DALException("查询失败,没有符合条件的数据");
        }
    }

    /**
     * 根据DTO 进行数据新增
     *
     * @param itemPushDTO 页面DTO
     * @return void
     * @author zhangxianchao
     * @since 2018/7/21 0021
     */
    @Override
    public Map<String, Object> insert(ItemPushDTO itemPushDTO) {
        //对对象进行赋值操作
        ItemPushPO itempushpo = itemPushDTO.transDtoToPo(ItemPushPO.class);
        //执行插入
        itemPushMapper.insert(itempushpo);
        //返回插入的值
        return itemPushMapper.selectById(itempushpo.getId()).toMaps();
    }

    /**
     * 根据DTO 及ID 进行数据更新
     *
     * @param id          主键ID
     * @param itemPushDTO 页面DTO
     * @return void
     * @author zhangxianchao
     * @since 2018/7/21 0021
     */
    @Override
    public Map<String, Object> update(Long id, ItemPushDTO itemPushDTO) {
        ItemPushPO itempushpo = itemPushMapper.selectById(id);
        //对对象进行赋值操作
        itemPushDTO.transDtoToPo(itempushpo);
        //执行更新
        itemPushMapper.updateById(itempushpo);
        return itempushpo.toMaps();
    }

    /**
     * 根据ID 进行删除数据
     *
     * @param id
     * @return void
     * @author zhangxianchao
     * @since 2018/8/25 0025
     */
    @Override
    public void deleteById(Long id) {
        int deleteCount = itemPushMapper.deleteById(id);
        logger.debug("删除数量：{}", deleteCount);
        if (deleteCount <= 0) {
            throw new DALException("删除失败,没有符合条件的数据");
        }
    }

    /**
     * 根据ids 进行删除
     *
     * @param ids
     * @return void
     * @author zhangxianchao
     * @since 2018/8/25 0025
     */
    @Override
    public void deleteBatchIds(String ids) {
        List<Long> longList = StringUtils.convertStrToArray(ids, ",", Long.class);
        int deleteCount = itemPushMapper.deleteBatchIds(longList);
        logger.debug("删除数量：{}", deleteCount);
        if (deleteCount != longList.size()) {
            throw new DALException("删除失败,没有符合条件的数据");
        }
    }

    @Override
    public void pustusertoyunyang(String userId) {
        ItemPushPO itempushpo = new ItemPushPO();
        itempushpo.setItemCode("pustUserToYUNYANG");
        itempushpo.setIsPush("0");
        itempushpo.setItemId(userId);
        itemPushMapper.insert(itempushpo);
    }

    @Override
    public void pustusertoyunyangtask() {
        LambdaQueryWrapper<ItemPushPO> query = new QueryWrapper<ItemPushPO>().lambda();
        query.eq(ItemPushPO::getItemCode, "pustUserToYUNYANG");
        query.eq(ItemPushPO::getIsPush, "0");
        List<ItemPushPO> itempushpolist = itemPushMapper.selectList(query);
        List<GetUserInfoByjwtVO> rlt = new ArrayList<>();
        for (ItemPushPO po : itempushpolist) {
            String userId = po.getItemId();
            if (!StringUtils.isNullOrEmpty(userId)) {
                GetUserInfoByjwtVO getUserInfoByjwtVO = userService.getuserinfobyuid(userId);
                if (null != getUserInfoByjwtVO) {
                    rlt.add(getUserInfoByjwtVO);
                } else {
                    List<GetUserInfoByjwtVO> list = userMapper.getUserInfoByUIdCMS(userId);
                    if (0 < list.size()) {
                        GetUserInfoByjwtVO vo = list.get(0);
                        vo.setRoleCode("");
                        rlt.add(vo);
                    }
                }

            }
        }
        YunyangVO param = new YunyangVO();
        RestServiceRequestDto requestDto = new RestServiceRequestDto();
        String url = "url" ;
        requestDto.setUrl(url);
        param.setList(rlt);
        String json = JSONObject.toJSONString(param);
        logger.info("同步员工给云漾" + json);
        requestDto.setBody(param);
        logger.info("开始获取数据");
        RestServiceResponseDto request2 = httpService.postJsonRequest(requestDto);

    }
}
